package com.junjie.checkin.controller;

import com.junjie.checkin.request.user.UserFrontRequest;
import com.junjie.checkin.response.Result;
import com.junjie.checkin.response.ResultStatus;
import com.junjie.checkin.response.front.UserFrontResponse;
import com.junjie.checkin.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 获取当前用户信息
     * @param request HTTP请求
     * @return 用户信息
     */
    @GetMapping("/me")
    public Result<UserFrontResponse> getUserInfo(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserFrontResponse userFrontResponse = userService.getCurrentUserInfo(token);
        return Result.getResult(ResultStatus.OK,userFrontResponse);
    }

    /**
     * 修改当前用户信息
     * @param userRequest 用户信息
     * @param request HTTP请求
     * @return 修改结果
     */
    @PutMapping("/me")
    public Result updateUserInfo(@Validated @RequestBody UserFrontRequest userRequest, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        userService.updateCurrentUserInfo(userRequest,token);
        return Result.getResult(ResultStatus.OK);
    }


}
